<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: front.func.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
defined('IN_TWCMS') || exit('Access Denied');

#自动加载模块类
function __autoload($class_name) {
	require TWCMS_FRONT.$class_name.'.lib.php';
}

#入口引导
function lead() {
	$model = array(2=>'page', 3=>'article', 4=>'picture', 4=>'product');
	switch ($_ENV->U[0]) {
		case $_ENV->G['url_cate']:
			if(!empty($_ENV->U[1]) && $_ENV->V['cid'] = intval($_ENV->U[1])) {
				$row = $_ENV->db->fetch_first('SELECT mid,dir,cate_tpl,count,upid,categoryname,seotitle,keywords,description FROM `'.TW_DBPRE.'category` WHERE cid='.$_ENV->V['cid']);
				if($row['mid'] == 1) {
					jsjump($row['dir']);
				}if($row) {
					$_ENV->table = $model[$row['mid']];
					$GLOBALS['html_total'] = $row['count'];
					$_ENV->V['upid'] = intval($row['upid']);
					$_ENV->V['title'] = $row['categoryname'];
					$_ENV->V['seotitle'] = $row['seotitle'];
					$_ENV->V['keywords'] = &$row['keywords'];
					$_ENV->V['description'] = &$row['description'];
					$_ENV->G['keywords'] = $_ENV->V['keywords'] ? $_ENV->V['keywords'] : $_ENV->V['title'];
					$_ENV->G['description'] = $_ENV->V['description'] ? $_ENV->V['description'] : $_ENV->G['webname'].', '.$_ENV->V['title'];
					$ret = $row['cate_tpl'];
				}
			}
			break;
		case $_ENV->G['url_show']:
			$_ENV->table = in_array($_ENV->U[1], $model) ? $_ENV->U[1] : 'article';
			$_ENV->V['id'] = intval($_ENV->table == 'article' ? $_ENV->U[1] : $_ENV->U[2]);
			$row = $_ENV->db->fetch_first('SELECT C.cid,C.upid,C.show_tpl FROM `'.TW_DBPRE.$_ENV->table.'` A LEFT JOIN `'.TW_DBPRE.'category` C ON A.cid=C.cid WHERE A.id='.$_ENV->V['id']);
			$_ENV->V['cid'] = intval($row['cid']);
			$_ENV->V['upid'] = intval($row['upid']);
			$ret = $row['show_tpl'];
			break;
		case $_ENV->G['url_comment']:
			break;
		case $_ENV->G['url_tags']:
			break;
		default:
			header('HTTP/1.1 404 Not Found');
			header("status: 404 Not Found");
			return '404.htm';
			$_ENV->R = explode('/', rm_suffix($_GET['u'], $_ENV->G['url_cate_suffix']));

			$row = $_ENV->db->fetch_first('SELECT cid,dir,mid,cate_tpl FROM  `'.TW_DBPRE.'category` WHERE dir='.S($_ENV->R[0]).' LIMIT 1');
			if($row) {
				$ret = $row['cate_tpl'];
				
				$_ENV->table = 'article';
				$_ENV->V['cid'] = $row['cid'];
				$_ENV->V['dir'] = $row['dir'];
				if(!empty($_ENV->R[1])) {
					if(substr($_ENV->R[1], 0, 5) == 'list_') {
						$_ENV->V['page'] = substr($_ENV->R[1], 5);
					}else{
						$_ENV->U[0] = $_ENV->G['url_show'];
						if($row['mid']==2) {
							$_ENV->U[1] = $_ENV->R[1];
						}else{
							$_ENV->U[1] = $model[$mid];
							$_ENV->U[2] = $_ENV->R[1];
						}
						$ret = lead();
					}
				}
			}else{
				if($row = $_ENV->db->fetch_first('SELECT mid,id,sole FROM  `'.TW_DBPRE.'urlsole` WHERE sole='.S($_ENV->R[0]).' LIMIT 1')) {
					$_ENV->V['sole'] = $row['sole'];
					$_ENV->U[0] = $_ENV->G['url_show'];
					if($row['mid']==2) {
						$_ENV->U[1] = $row['id'];
					}else{
						$_ENV->U[2] = $_ENV->R[1];
					}
					$ret = lead();
				}
			}
	}
	
	if(empty($ret)) {
		header('HTTP/1.1 404 Not Found');
		header("status: 404 Not Found");
		return '404.htm';
	}
	return $ret;
}

#您的位置
function place($upid) {
	$field = '';
	if($_ENV->G['ishtml']) {	#静态
		$field = ',dir,parentdir,cate_url';
	}elseif($_ENV->G['isfhtml'] && $_ENV->G['custom_cate']) {	#分类页伪静态高级
		$field = ',dir';
	}

	$arr = array();
	while($row = $_ENV->db->fetch_first('SELECT cid,upid,categoryname'.$field.' FROM `'.TW_DBPRE.'category` WHERE cid='.$upid)) {
		$upid = $row['upid'];

		if($_ENV->G['ishtml']) {	#静态
			$url = dirname(rep_url($row['cate_url'], $row['parentdir'], $row['dir'], $row['cid'])).'/';
		}elseif($_ENV->G['isfhtml'] && $_ENV->G['custom_cate']) { #分类页伪静态高级
			$url = $row['dir'].'/';
		}else{	#伪静态/动态
			$url = (empty($_ENV->G['isfhtml']) ? 'index.php?u=' : '').$_ENV->G['url_cate'].'-'.$row['cid'].$_ENV->G['url_cate_suffix'];
		}
		array_unshift($arr, array('categoryname'=> $row['categoryname'], 'url'=> $_ENV->G['webdir'].$url, 'cid'=> $row['cid']));
	}
	return $arr;
}

#替换生成静态URL
function rep_url($url, $parentdir, $dir, $cid) {
	$url = str_replace('{parentdir}', $parentdir, $url);
	$url = str_replace('{dir}', $dir, $url);
	return $_ENV->G['html_dir'].trim(str_replace('{cid}', $cid, $url), '/');
}

#替换生成静态时内容页URL
function rep_show_url($url, $time, $id) {
	$url = str_replace('{y}', date('Y', $time), $url);
	$url = str_replace('{m}', date('m', $time), $url);
	$url = str_replace('{d}', date('d', $time), $url);
	return str_replace('{id}', $id, $url);
}

#删除URL后辍
function rm_suffix($url, $suffix) {
	$len = strlen($suffix);
	if(substr($url, -$len) == $suffix) {
		return substr($url, 0, -$len);
	}
	return $url;
}